package com.xilixili.miaomi.wechat.network;

import android.graphics.Bitmap;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xilixili.miaomi.wechat.entity.TweetInfo;
import com.xilixili.miaomi.wechat.global.Contacts;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.BitmapCallback;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Request;

/**
 * Created by liumin on 2017/12/20.
 */

public class NetworkManager {

    private String TAG = getClass().getSimpleName();
    TweetsLister lister;
    private List<TweetInfo> list = null;
    private int max_return = 5;
    private int begin_return = 0;

    public class MyStringCallback extends StringCallback
    {
        @Override
        public void onBefore(Request request, int id)
        {

        }

        @Override
        public void onAfter(int id)
        {

        }

        @Override
        public void onError(Call call, Exception e, int id)
        {
            e.printStackTrace();

        }

        @Override
        public void onResponse(String response, int id)
        {
            Log.e(TAG, "onResponse：complete ");

            if(list == null) {
                list = new ArrayList<TweetInfo>();

                try {
                    Log.d(TAG,"begin gson");
                    Gson gson = new Gson();
                    list = gson.fromJson(new String(response.getBytes(),"UTF-8"), new TypeToken<List<TweetInfo>>(){}.getType());
                    if(list != null) {
                        Log.d(TAG, "list.size " + list.toString());
                    } else {
                        Log.d(TAG, "list.size = null");
                    }
                }catch(Exception e) {
                    e.printStackTrace();
                }
                list = checkValid(list);
                Log.d(TAG, "onResponse：init date end");
            }

            List<TweetInfo> returnList = null;
            if(list.size() - begin_return > max_return) {
                returnList = list.subList(begin_return, begin_return + max_return);
                begin_return += max_return;
            } else {
                returnList = list.subList(begin_return, list.size());
                begin_return = list.size();
            }

            lister.getTweetInfos(returnList);
        }

        @Override
        public void inProgress(float progress, long total, int id)
        {
            Log.e(TAG, "inProgress:" + progress);
        }
    }

    public class MyBitmapCallback extends BitmapCallback{
        private int position;

        public MyBitmapCallback(int position) {
            this.position = position;
        }

        @Override
        public void onError(Call call, Exception e, int id) {
            Log.d(TAG, "MyBitmapCallback exception " + e.getMessage());
        }

        @Override
        public void onResponse(Bitmap response, int id) {
            lister.getBitMap(response, position);
        }
    }

    private List<TweetInfo> checkValid(List<TweetInfo> tweetInfos) {
        if(tweetInfos == null) {
            return null;
        }

        List<TweetInfo> resultInfos = new ArrayList<>();
        for(TweetInfo info : tweetInfos) {
            Log.d("TweetInfo", "info " + toString() + ", ret " + info.checkValid());
            if(info.checkValid()) {
                resultInfos.add(info);
            }
        }
        return resultInfos;
    }

    public void getTweetInfos(TweetsLister lister){
        this.lister = lister;
        String url = Contacts.TWEETINFOS_URL;
        OkHttpUtils
                .get()
                .url(url)
                .id(100)
                .build()
                .execute(new MyStringCallback());
    }

    public void getBitMap(TweetsLister lister, String url, int position) {
        this.lister = lister;
        OkHttpUtils
                .get()//
                .url(url)//
                .tag(this)//
                .build()//
                .connTimeOut(20000)
                .readTimeOut(20000)
                .writeTimeOut(20000)
                .execute(new MyBitmapCallback(position));
    }

    public interface TweetsLister{
        void getTweetInfos(List<TweetInfo> list);

        void getBitMap(Bitmap bitmap, int position);
    }
}
